<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @addtask="addtaskFn" :list="list"></TodoHeader>
    <TodoMain :list="showList" @sub="subFn"></TodoMain>
    <TodoFooter :list="list" @changeType="changFn" :type="getSel" :len="showList.length" @qingchu="qingchuFn">
    </TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";


export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('todolsti')) || [],
      getSel: 'all',
    };
  },
  //通过点击传参来改变getSel的值，在通过计算属性判断all yes no 的操作 从新渲染页面
  computed: {
    showList() {
      if (this.getSel === 'yes') {
        return this.list.filter(item => item.isDone)
      } else if (this.getSel === 'no') {
        return this.list.filter(item => !item.isDone)
      } else {
        return this.list
      }
    },
  },
  methods: {
    //添加
    addtaskFn(task) {
      this.list.push({
        id: this.list.length ? this.list.at(-1).id + 1 : 100,
        name: task,
        isDone: true,
      })
    },
    //删除
    subFn(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    changFn(type) {
      //点击之后子组件传过all或者yes.no 计算属性进行判断在从新渲染页面
      this.getSel = type
    },

    qingchuFn() {
      //filter寻找数组的每一项这是时候取反让他寻找假的属性返回
      this.list = this.list.filter(item => !item.isDone)
    }
  },

  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem('todolsti', JSON.stringify(this.list))
      }
    }
  }
};
</script>